<?php
require_once 'BaseController.php';
require_once APPPATH.'/object/project.php';
require_once APPPATH.'/models/project_model.php';
require_once APPPATH.'/models/servicetype_model.php';
require_once APPPATH.'/models/customer_model.php';

/**
 * Projects Controller
 * @author cuonglv@vietincom.com
 */

class Projects extends BaseController{
    
    function __construct(){
        $this->data = parent::__construct();
        $this->obj = new Project();
    }

    /**
     * Index action
     * @param   none
     * @author  cuonglv@vietincom.com
     */
    function index(){
        try
        {
            $this->load->view('/project/index',$this->data);
        }
        catch(Exception $ex)
        {
            
        }
    }
    
    function create(){
        try
        {
            if($this->_isPost())
            {
                // get post data
                $this->obj = $this->_getInputData();
                
                if($this->data['langType'] == 'vn')
                {
                    $this->obj->description_vn = $this->input->post('description');
                    $this->obj->name_vn = $this->input->post('name');
                }
                else
                {
                    $this->obj->description_en = $this->input->post('description');
                    $this->obj->name_en = $this->input->post('name');
                }

                $this->obj->created_by = 1;
                $this->obj->approved_by = null;

                //for upload file 
                $config['upload_path']      = '.'.DIRECTORY_SEPARATOR.'uploads';
                $config['allowed_types']    = 'gif|jpg|png|bmp|jpeg';
                $config['max_dsize']         = '1000';
                //$config['max_width']        = '1024';
                //$config['max_height']       = '768';
                $config['overwrite']        = FALSE;
                $config['file_name']        = time().'_user_id'.".jpg";
                $config['remove_spaces']    = TRUE;
;
                $this->load->library('upload', $config);


                if ( ! $this->upload->do_upload("image"))
                {
                    $error = array('error' => $this->upload->display_errors());var_dump($error);die();

                }
                else
                {
                    $upload_image = $this->upload->data();
                    //var_dump($upload_image);die();
                    $this->obj->image = $config['file_name'];
                    
                    // insert data
                    $mdProject = new Model_Project();
                    $mdProject->_insert($this->obj);
                    redirect('/projects');
                }                
            }
            else
            {
                $mdServiceType = new Model_Service_Types();
                $this->data['serviceTypes'] = $mdServiceType->_getAll();

                $mdCustomer = new Customer_model();
                $this->data['customers'] = $mdCustomer->_getAll();
            }

            $this->load->view('/project/create',$this->data);
        }
        catch(Exception $ex)
        {

        }
    }

    function edit(){
        try
        {
            $this->load->view('/project/create',$data);
        }
        catch(Exception $ex)
        {

        }
    }

    function delete(){
        try
        {

        }
        catch(Exception $ex)
        {

        }
    }
}

?>